<?php
/*
 * Created on 2010-8-28
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 class BookForm extends CFormModel{
 	
 	public $categoryId,$name,$author,$publish,$publish_datetime,$publish_version;
 	public $print_version,$print_datetime,$ISBN,$words,$pages;
 	public $format,$paper,$package,$market_price,$small_pic,$big_pic,$is_recommend,$price;
 	public $author_intro,$book_intro,$book_menu;
 	public $big_pic_file,$small_pic_file;
 	
 	public function rules(){
 		return array(
			array('categoryId,name,author,publish,publish_datetime,publish_version,print_version,print_datetime,ISBN,words,pages,format,paper,package,market_price,small_pic,big_pic,is_recommend,price,author_intro,book_intro,book_menu','required','on'=>'add'),
 		);
 	}
 	
 	public function attributeLabels(){
 		return array(
 			'categoryId'=>'图书分类',
 			'name'=>'图书名称',
 			'author'=>'图书作者',
 			'publish'=>'出版社',
 			'publish_datetime'=>'出版时间',
 			'publish_version'=>'版次',
 			'print_datetime'=>'印刷时间',
 			'print_version'=>'印次',
 			'ISBN'=>'ISBN',
 			'words'=>'字数',
 			'pages'=>'页数',
 			'format'=>'开本',
 			'paper'=>'纸张',
 			'package'=>'包装',
 			'market_price'=>'市场价',
 			'small_pic'=>'缩略图',
 			'big_pic'=>'原图',
 			'is_recommend'=>'编辑推荐',
 			'price'=>'定价',
 			'author_intro'=>'作者简介',
 			'book_intro'=>'图书简介',
 			'book_menu'=>'图书目录',
 			
 		);
 	}
 	
	/**
	 * 保存图书
	 */
	 public function saveBook(){
		 $book = new UrBook;
		 $book->attributes = $this->attributes;
		 $big_path = bp().'/../attachment/bookcover/big/';
		 $small_path = bp().'/../attachment/bookcover/small/';
		 $book->big_pic = $this->big_pic_file===null?$big_path.'default_big_pic.jpg':$this->saveImage($big_path,$this->big_pic_file);
		 $book->small_pic = $this->big_pic_file===null?$small_path.'default_small_pic.jpg':$this->saveImage($small_path,$this->small_pic_file);
		 $book_extra = new UrBookExtra;
		 $book_extra->attributes = $this->attributes;
		 var_dump($book->attributes);
		 if($book->save()){
			 $book_extra->id = $book->id;
			 if($book_extra->save())
			 return true;
			 }else
			 return false;
		 }
		 
	/**
	 * 保存图书封面
	 * $path 图书封面保存路径 结尾有'/'
	 * $file CUploadedFile instance
	 */
	 public function saveImage($path,$file){
		 $filename = $path.time().'big_.'.$file->extensionName;
		 if($file->saveAs($filename)){
			 return $filename;
			 }else{
			return false;	 
			}
		 }
 	
 } 
?>
